#!/usr/bin/python
# -*- coding: utf-8 -*-

import re
import sys
import string
import glob
import file_id
from pepe.glib import file_hash

def reflow(text):
    """ Reflow text routine

    operation rules:
    - remove hard newlines HN if not at paragraph end
    - keep HN at listings and headers (creole wiki header ... == ===)
    - empty space at line beginning and ending is removed
    - repeated empty space characters are reduced to one
    
    implementation notes:
    - order of conditions applied is important, change it carefully
    """
    res = []
    for line in string.split(text, '\n'):
        line = re.sub('\n', '', line)
        line = re.sub(' {2,}', ' ', line)
        line = string.strip(line)

        # empty fist line
        if line == '' and len(res) == 0:
            res.append('\n')

        # only one empty line between lines allowed
        elif line == '' and res[-1] == '\n':
            pass

        # remove 'page number' lines 
        # ... can no do this because some authors use plain numbers for chapters (e.g.JLCaree)
        #elif re.sub('\d', '', line) == '':
        #    pass

        # header line
        elif line[:1] in ('='):
            res.append('\n' + line + '\n\n')

        # line ending with 'end' character
        elif line[-1] in (':'):
            res.append(line + '\n')

        # line (probably a listing)
        #len(line) < 70 and
        #elif line[:1] in ('(', '[', '/', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '*', '-'):
        elif line[:1] in ('(', '[', '/', '*', '-'):
            res.append(line + '\n')

        # line ending with 'end' character
        elif line[-1] in ('!','.','"','?'):
            res.append(line + '\n\n')

        # very short line
        elif len(line) < 30:
            res.append(line + '\n')

        else:
            res.append(line + ' ')

    ret = string.join(res, '')
    ret = re.sub('\n{3,}', '\n\n', ret)
    return ret

def reflow_file(ifile):
    """ """
    fi = open(ifile, 'r')
    t = fi.read()
    fi.close()
    fo = open(f+'_', 'w')
    fo.write(reflow(t))
    fo.close()

def char_generator(wide=3):
    """ """
    for a in string.letters[:26]:
        for b in string.letters[:26]:
            for c in string.letters[:26]:
                if wide == 3:
                    yield string.lower("%s%s%s" % (a,b,c))
                elif wide == 4:
                    for d in string.letters[:26]:
                        yield string.lower("%s%s%s%s" % (a,b,c,d))
                else:
                    print "Unsupported parameter value ! %s" % (wide)
                    sys.exit(2)

def txt_abc(infl, useid=False, wide=3, charset='utf-8'):
    ''' Text referencing utility

    documentation: abc.readme.abc
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    '''

    lastmodified = '20090919-2344'

    fid = file_id.get(infl)
    fileid = '<a href="http://flib/%s" target="_top">http://flib/%s</a><br>' % (fid, fid)
    iter = char_generator(int(wide))

    html = """<?xml version="1.0" encoding="%s" standalone="no"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <meta http-equiv="Content-Type" content="text/html;charset=%s" />
    <head>
    <title>%s</title>
    </head>
    <body>
    %s
    """ % (charset, charset, fileid, fileid)
    html = re.sub('\n {1,}', '\n', html)
    of = open(infl + '.ebk.html', 'w')
    of.write(html)
    data = open(infl, 'r').read()
    
    data = re.sub('<', '&lt;', data)
    data = re.sub('>', '&gt;', data)
    data = re.sub("‘", "'", data)
    data = re.sub("’", "'", data)
    data = re.sub("’", "'", data)
    data = re.sub("’", "'", data)
    data = re.sub("‘", "'", data)
    data = re.sub("’", "'", data)
    data = re.sub('“', "'", data)
    data = re.sub('”', "'", data)
    data = re.sub('—', ' - ', data)
    data = re.sub('…', '...', data)
    data = re.sub(' ', '', data)
    data = re.sub('é', 'e', data)
    data = re.sub('í', 'i', data)
    data = re.sub('ń', 'n', data)
    data = re.sub('ó', 'o', data)
    data = re.sub('ç', 'c', data)
    data = re.sub('ü', 'u', data)
    data = re.sub('Ü', 'U', data)
    data = re.sub('ö', 'o', data)

    data = reflow(data)

    #test = open('test.out', 'w'); test.write(data); test.close()

    for line in string.split(data, '\n'):
        if re.sub('\s{1,}', '', line) not in (''):

            if line [:3] == '\xEF\xBB\xBF':
                pass

            # creole header formatting ... == h2 === h3 ==== h4
            elif line[:5] == '==== ':
                i = iter.next()
                of.write('<h4>%s</h4><!-- %s --><a id="%s">\n' % (line[4:], i, i))

            elif line[:4] == '=== ':
                i = iter.next()
                of.write('<h3>%s</h3><!-- %s --><a id="%s">\n' % (line[3:], i, i))

            elif line[:3] == '== ':
                i = iter.next()
                of.write('<h2>%s</h2><!-- %s --><a id="%s">\n' % (line[2:], i, i))

            else:
                if useid:
                    i = iter.next()
                    of.write('<a id="%s">\n<p>&nbsp;&nbsp;%s -%s-</p>\n<p>&nbsp;</p>\n' % (i, line, i)) # openinkpot os
                    #of.write("<a id="%s">\n<p>&nbsp;%s -%s-</p>\n\n" % (i, line, i)) # hanlin os
                else:
                    of.write('<p>&nbsp;%s</p>\n<p>&nbsp;</p>\n' % (line)) # openinkpot os
                    #of.write("<p>&nbsp;%s</p>\n" % (line)) # hanlin os
            i = None
        else:
            of.write('\n')

    of.write("<br>$source file sha1 %s$\n" % (file_hash(infl, type='sha1')[:41]))
    of.write('<br>$source text transformed by txt_abc.py (modified %s by vp)$\n' % (lastmodified))
    of.write('</body>\n</html>\n')
    of.close()

if __name__ == "__main__":
    ''' '''
    if len(sys.argv) == 1:
        for f in glob.glob('*.abc'):
            txt_abc(f, useid=True)
    else:
        if sys.argv[1] == '4':
            for f in glob.glob('*.abc'):
                txt_abc(f, useid=True, wide=sys.argv[1])
        if sys.argv[1] == 'reflow':
            for f in glob.glob('*.abc'):
                reflow_file(f)
